#include "gd32f30x.h"
#include "DevicesDelay.h"
#include "DevicesPower.h"



void vPowerInit(void)
{
    /* enable the GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_GPIOD);
    rcu_periph_clock_enable(RCU_GPIOE);

    vPowerClose(POWER_CHANNEL_ALL);

    /* configure GPIO pin */
    gpio_init(POWER_SYSTEM_PD_GPIO_Port,    GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, POWER_SYSTEM_PD_Pin);

    gpio_init(POWER_DC24V_OUT_GPIO_Port,    GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, POWER_DC24V_OUT_Pin);
    gpio_init(POWER_DC12V_OUT_GPIO_Port,    GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, POWER_DC12V_OUT_Pin);
    gpio_init(POWER_USB_OUT_GPIO_Port,      GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, POWER_USB_OUT_Pin);
    gpio_init(POWER_ADS24V_OUT_GPIO_Port,   GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, POWER_ADS24V_OUT_Pin);
}

void vPowerOpen(uint16_t usNumber)
{
    if(usNumber & POWER_CHANNEL_SYSTEM_PD)
    {
        POWER_SYSTEM_PD_ENABLE();
    }

    if(usNumber & POWER_CHANNEL_USB_OUT)
    {
        POWER_USB_OUT_ENABLE();
    }
    if(usNumber & POWER_CHANNEL_ADS24V_OUT)
    {
        POWER_ADS24V_OUT_ENABLE();
    }
    if(usNumber & POWER_CHANNEL_DC12V_OUT)
    {
        POWER_DC12V_OUT_ENABLE();
    }
    if(usNumber & POWER_CHANNEL_DC24V_OUT)
    {
        POWER_DC24V_OUT_ENABLE();
    }
}

void vPowerClose(uint16_t usNumber)
{
    if(usNumber & POWER_CHANNEL_SYSTEM_PD)
    {
        POWER_SYSTEM_PD_DISABLE();
    }

    if(usNumber & POWER_CHANNEL_USB_OUT)
    {
        POWER_USB_OUT_DISABLE();
    }
    if(usNumber & POWER_CHANNEL_ADS24V_OUT)
    {
        POWER_ADS24V_OUT_DISABLE();
    }
    if(usNumber & POWER_CHANNEL_DC12V_OUT)
    {
        POWER_DC12V_OUT_DISABLE();
    }
    if(usNumber & POWER_CHANNEL_DC24V_OUT)
    {
        POWER_DC24V_OUT_DISABLE();
    }
}
